package com.common.result;

import cn.hutool.core.date.SystemClock;
import cn.hutool.json.JSONObject;
import com.common.enums.error.ResultEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: stefanie-zy
 * @time: 2024/1/27-10:47
 * @description: 接口返回值
 */
@Data
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Schema(name = "接口返回值")
public class Result<T> {

    @Schema(description = "响应码")
    private Integer code;
    @Schema(description = "响应描述")
    private String message;
    @Schema(description = "响应参数")
    private T data;
    @Schema(description = "响应时间")
    private Long timestamp;


    public static Result<?> success() {
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), null, SystemClock.now());
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data, SystemClock.now());
    }

    public static Result<?> fail() {
        return new Result<>(ResultEnum.S_FAIL.getCode(), ResultEnum.S_FAIL.getMessage(), null, SystemClock.now());
    }

    public static <T> Result<T> fail(T data) {
        return new Result<>(ResultEnum.S_FAIL.getCode(), ResultEnum.S_FAIL.getMessage(), data, SystemClock.now());
    }

    public static Result<?> fail(Integer code, String message) {
        return new Result<>(code, message, null, SystemClock.now());
    }

    public static Result<?> fail(ResultEnum resultEnum) {
        return new Result<>(resultEnum.getCode(), resultEnum.getMessage(), null, SystemClock.now());
    }

    public static Result<?> fail(Integer code, String message, JSONObject data) {
        return new Result<>(code, message, data, SystemClock.now());
    }

}
